import React, { useState } from 'react';
import { Upload, message, Button } from 'antd';
import { UploadOutlined } from '@ant-design/icons';
import * as api from '../../services/api';

const FileUpload = ({ onUploadSuccess }) => {
    const [uploading, setUploading] = useState(false);

    // 处理文件上传
    const handleUpload = async (file) => {
        const formData = new FormData();
        formData.append('file', file);
        setUploading(true);

        try {
            const result = await api.uploadFile(formData);
            if (result.code === 200) {
                message.success('文件上传成功');
                if (onUploadSuccess) {
                    onUploadSuccess();  // 通知父组件刷新文件列表
                }
            }
            return Upload.LIST_IGNORE;
        } catch (error) {
            message.error('文件上传失败');
            return Upload.LIST_IGNORE;
        } finally {
            setUploading(false);
        }
    };

    return (
        <Upload
            beforeUpload={handleUpload}
            showUploadList={false}
        >
            <Button 
                icon={<UploadOutlined />} 
                loading={uploading}
                type="primary"
            >
                点击上传文件
            </Button>
        </Upload>
    );
};

export default FileUpload; 